/*
2021-7-21
https://www.acwing.com/problem/content/905/
*/

#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;
const int N=105;
int n,m;
int level[N],dist[N];
int g[N][N];
int w[N][N];
bool st[N];

int dij(int down,int up)
{
    memset(dist,0x3f,sizeof dist);
    memset(st,false,sizeof st);
    dist[0]=0;
    
    for(int i=1;i<=n+1;i++)
    {
        int t=-1;
        for(int j=0;j<=n;j++)
        {
            if(!st[j] && (t==-1 || dist[t]>dist[j]))
                t=j;
        }
        
        st[t]=true;
        
        for(int j=1;j<=n;j++)
        {
            if(level[j]>=down && level[j]<=up)
                dist[j]=min(dist[j],dist[t]+w[t][j]);
        }
        
    }
    return dist[1];
}

int main()
{
    cin>>m>>n;
    memset(w,0x3f,sizeof w);
    for(int i=0;i<=n;i++) w[i][i]=0;
    
    for(int i=1;i<=n;i++)
    {
        int price,cnt;
        cin>>price>>level[i]>>cnt;
        w[0][i]=min(w[0][i],price);
        
        while(cnt--)
        {
            int t,v;
            cin>>t>>v;
            w[t][i]=min(w[t][i],v);
        }
    }
    
    int res=0x3f3f3f3f;
    for(int i=level[1]-m;i<=level[1];i++) res=min(res,dij(i,i+m));
    
    cout<<res<<endl;
    
    return 0;
}

